import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema';
import { Schema as ApplicationOptions, Style } from '@schematics/angular/application/schema';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { getProjectFromWorkspace, getProjectMainFile, parseSourceFile } from '@angular/cdk/schematics';
import { Tree } from '@angular-devkit/schematics';
import { getWorkspace } from '@schematics/angular/utility/workspace';
import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils';

export const workspaceOptions: WorkspaceOptions = {
  name: 'workspace',
  newProjectRoot: 'projects',
  version: '6.0.0',
};

export const appOptions: ApplicationOptions = {
  name: 'bar',
  inlineStyle: false,
  inlineTemplate: false,
  routing: false,
  style: Style.Css,
  skipTests: false,
  skipPackageJson: false,
};

export function createTestRunner(): SchematicTestRunner {
  const collectionPath = path.join(__dirname, '../collection.json');
  return new SchematicTestRunner('schematics', collectionPath);
}

export async function createTestTree(runner: SchematicTestRunner): Promise<UnitTestTree> {
  let appTree: UnitTestTree;
  appTree = await runner.runExternalSchematicAsync('@Schematics/angular', 'workspace', workspaceOptions).toPromise();
  appTree = await runner.runExternalSchematicAsync('@Schematics/angular', 'application', appOptions, appTree).toPromise();
  return appTree;
}

export async function resolveTree(_tree: Tree) {
  const workspace = await getWorkspace(_tree);
  let next = workspace.projects.entries().next();
  const project = getProjectFromWorkspace(workspace, next.value[0]);
  const appModulePath = getAppModulePath(_tree, getProjectMainFile(project));
  const moduleSource = parseSourceFile(_tree, appModulePath);
  return {
    workspace,
    project,
    appModulePath,
    moduleSource,
    sourceRoot: project.sourceRoot,
  };
}

export const defaultAppComponentContent = `import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'bar';
}
`;

export const defaultFiles = [
  '/README.md',
  '/.editorconfig',
  '/.gitignore',
  '/angular.json',
  '/package.json',
  '/tsconfig.json',
  '/.vscode/extensions.json',
  '/.vscode/launch.json',
  '/.vscode/tasks.json',
  '/projects/bar/.browserslistrc',
  '/projects/bar/karma.conf.js',
  '/projects/bar/tsconfig.app.json',
  '/projects/bar/tsconfig.spec.json',
  '/projects/bar/src/favicon.ico',
  '/projects/bar/src/index.html',
  '/projects/bar/src/main.ts',
  '/projects/bar/src/polyfills.ts',
  '/projects/bar/src/styles.css',
  '/projects/bar/src/test.ts',
  '/projects/bar/src/assets/.gitkeep',
  '/projects/bar/src/environments/environment.prod.ts',
  '/projects/bar/src/environments/environment.ts',
  '/projects/bar/src/app/app.module.ts',
  '/projects/bar/src/app/app.component.css',
  '/projects/bar/src/app/app.component.html',
  '/projects/bar/src/app/app.component.spec.ts',
  '/projects/bar/src/app/app.component.ts',
];

